#include "PictureWidget.h"
#include "ui_PictureWidget.h"
#include "picturemodel.h"
#include "ThumbnailProxyModel.h"

PictureWidget::PictureWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PictureWidget),
    mModel(nullptr),
    mSelectionModel(nullptr)
{
    ui->setupUi(this);

    connect(ui->backButton, &QPushButton::clicked, this, &PictureWidget::backToGallery);
    connect(ui->deleteButton, &QPushButton::clicked, this, &PictureWidget::deletePicture);
    connect(ui->previousButton, &QPushButton::clicked, this, &PictureWidget::loadPreviousPicture);
    connect(ui->nextButton, &QPushButton::clicked, this, &PictureWidget::loadNextPicture);
}

PictureWidget::~PictureWidget()
{
    delete ui;
}

void PictureWidget::setModel(ThumbnailProxyModel *model)
{
    mModel = model;
}

void PictureWidget::setSelectionModel(QItemSelectionModel *selectionModel)
{
    mSelectionModel = selectionModel;
    connect(mSelectionModel, &QItemSelectionModel::selectionChanged,
            this, &PictureWidget::loadPicture);
}

void PictureWidget::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    updatePicturePixmap();
}

void PictureWidget::deletePicture()
{
    if( !mSelectionModel->currentIndex().isValid() ) {
        return;
    }
    int row = mSelectionModel->currentIndex().row();
    mModel->removeRow(row);

    QModelIndex previousIndex = mModel->index(row-1, 0);
    if( previousIndex.isValid() ) {
        mSelectionModel->setCurrentIndex(previousIndex,
                                         QItemSelectionModel::SelectCurrent);
        return;
    }

    QModelIndex nextIndex = mModel->index(row, 0);
    if( nextIndex.isValid() ) {
        mSelectionModel->setCurrentIndex(nextIndex,
                                         QItemSelectionModel::SelectCurrent);
        return;
    }
}

void PictureWidget::loadPreviousPicture()
{
    if(mSelectionModel->selectedIndexes().isEmpty()) {
        return;
    }

    int row = mSelectionModel->currentIndex().row();
    QModelIndex previousIndex = mModel->index(row-1, 0);
    if( previousIndex.isValid() ) {
        mSelectionModel->setCurrentIndex(previousIndex,
                                         QItemSelectionModel::SelectCurrent);
        return;
    }
}

void PictureWidget::loadNextPicture()
{
    if(mSelectionModel->selectedIndexes().isEmpty()) {
        return;
    }

    int row = mSelectionModel->currentIndex().row();
    QModelIndex nextIndex = mModel->index(row+1, 0);
    if( nextIndex.isValid() ) {
        mSelectionModel->setCurrentIndex(nextIndex,
                                         QItemSelectionModel::SelectCurrent);
        return;
    }
}

void PictureWidget::loadPicture(const QItemSelection &selected)
{
    if(selected.indexes().isEmpty())
    {
        ui->nameLabel->setText("");
        ui->pictureLabel->setPixmap(QPixmap());
        ui->deleteButton->setEnabled(false);
        return;
    }

    QModelIndex index = selected.indexes().first();
    mPixmap = mModel->data(index, Qt::DecorationRole).value<QPixmap>();

    ui->nameLabel->setText(mModel->data(index, Qt::DisplayRole).toString());
    updatePicturePixmap();

    ui->previousButton->setEnabled(index.row() > 0);
    ui->nextButton->setEnabled(index.row() < (mModel->rowCount() - 1));
    ui->deleteButton->setEnabled(true);
}

void PictureWidget::updatePicturePixmap()
{
    if( mPixmap.isNull() ) {
        return;
    }

    ui->pictureLabel->setPixmap(mPixmap.scaled(ui->pictureLabel->size(), Qt::KeepAspectRatio));
}
